package com.iexbuy.ihg.ui.fragment;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.iexbuy.ihg.R;
import com.iexbuy.ihg.adapter.DealHistoryDetailListAdapter;
import com.iexbuy.ihg.common.CommUtil;
import com.iexbuy.ihg.common.ExtraKeyConstant;
import com.iexbuy.ihg.entity.DealHistoryDetailListObjectResponce;
import com.iexbuy.ihg.entity.DealHistoryDetailObject;
import com.iexbuy.ihg.entity.XiaoFeiOrderConfirmObject;
import com.iexbuy.ihg.entity.YunItemListObject;
import com.iexbuy.ihg.net.ServiceHelper;
import com.iexbuy.ihg.utils.JsonUtils;
import com.iexbuy.ihg.utils.ToastUtils;
import com.jingchen.pulltorefresh.PullToRefreshLayout;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.view.annotation.ViewInject;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;

public class WeDeOrdersFragment extends BaseFragment {
	
	private static WeDeOrdersFragment homeFragmentInstent;

	@ViewInject(R.id.msgListLayout)
	private PullToRefreshLayout msgListRefreshLayout;
	@ViewInject(R.id.listView)
	private ListView listView;

	private List<DealHistoryDetailObject> dealHistoryDetailObjects = new ArrayList<DealHistoryDetailObject>();
	private List<DealHistoryDetailObject> dealHistoryDetailPay = new ArrayList<DealHistoryDetailObject>();
	private int pageNo = 0;
	private com.iexbuy.ihg.adapter.DealHistoryDetailListAdapter dealHistoryDetailListAdapter;

	private static boolean closeInd = false;
	private static String status;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		homeFragmentInstent = this;
	}

	@Override
	protected View _onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View rootView = inflater.inflate(R.layout.fragment_wede_orders, container, false);
		ViewUtils.inject(this, rootView);
		initView(rootView);
		return rootView;
	}

	private void initView(View rootView) {
		mBaseActivity.setTitle(getResources().getString(R.string.we_de_orders));
		mBaseActivity.setTitleLayoutVisibility(true);
		mBaseActivity.setHomeActivity(false);

		msgListRefreshLayout.setOnRefreshListener(new PullToRefreshLayout.OnRefreshListener() {
			@Override
			public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
				initListData();
			}

			@Override
			public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
				loadMore();
			}
		});
		msgListRefreshLayout.autoRefresh();
	}

	private void loadMore() {
		pageNo ++ ;
		initListData();
	}

	public void onResume() {
		super.onResume();
		if (closeInd) {
			closeInd = false;
			mBaseActivity.finish();
		}
		msgListRefreshLayout.autoRefresh();
	}


	public void initListData() {

		HashMap<String, String> paramMap = new HashMap<String, String>();
		paramMap.put("pageNo", String.valueOf(pageNo));
		paramMap.put("pageSize", String.valueOf(ExtraKeyConstant.COMMON_PAGE_SIZE));
		ServiceHelper.send(mBaseActivity, ServiceHelper.GET_MY_DEALS, paramMap,
				new RequestCallBack<String>() {
					@Override
					public void onStart() {
						msgListRefreshLayout.refreshFinish(PullToRefreshLayout.REFRESHING);
					}

					@Override
					public void onCancelled() {
						msgListRefreshLayout.refreshFinish(PullToRefreshLayout.DONE);
					}

					@Override
					public void onSuccess(ResponseInfo<String> responseInfo) {
						msgListRefreshLayout.refreshFinish(PullToRefreshLayout.SUCCEED);
						DealHistoryDetailListObjectResponce rep = JsonUtils.parseJsonStrToObject(JsonUtils.getJsonSubStringByKey(responseInfo.result, "body"), DealHistoryDetailListObjectResponce.class);
						if (pageNo == 0) {
							dealHistoryDetailObjects.clear();
						}
						dealHistoryDetailObjects.addAll(rep.getData());
						initdealHistoryDetailObjectView();
					}

					@Override
					public void onFailure(HttpException error, String msg) {
						msgListRefreshLayout.refreshFinish(PullToRefreshLayout.FAIL);
						ToastUtils.toast(mBaseActivity, msg);
					}
				});
	}

	@Override
	public void onStop() {
		super.onStop();
		status=null;
	}

	private void initdealHistoryDetailObjectView() {

		//对已取消订单
		if (dealHistoryDetailPay!=null){
			dealHistoryDetailPay.clear();
		}

		if (status!=null){//只取待支付订单
			for (int i = 0; i < dealHistoryDetailObjects.size(); i++) {
				DealHistoryDetailObject dealHistoryDetailObject = dealHistoryDetailObjects.get(i);
				if (dealHistoryDetailObject.getStatus().equals(status)) {
					dealHistoryDetailPay.add(dealHistoryDetailObject);
				}
			}
		}else {
			for (int i = 0; i < dealHistoryDetailObjects.size(); i++) {
				DealHistoryDetailObject dealHistoryDetailObject = dealHistoryDetailObjects.get(i);
				if (!dealHistoryDetailObject.getStatus().equals("5")) {
					dealHistoryDetailPay.add(dealHistoryDetailObject);
				}
			}
			Collections.sort(dealHistoryDetailPay, new Comparator<DealHistoryDetailObject>() {
				@Override
				public int compare(DealHistoryDetailObject t1, DealHistoryDetailObject t2) {
					return new BigDecimal(t1.getStatus()).compareTo(new BigDecimal(t2.getStatus()));
				}
			});

		}
		if (dealHistoryDetailListAdapter == null) {
			dealHistoryDetailListAdapter = new DealHistoryDetailListAdapter(mBaseActivity, dealHistoryDetailPay);

			listView.setAdapter(dealHistoryDetailListAdapter);
			listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
				@Override
				public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
					OrderDetailFrament.setXiaoFeiOrderConfirmObject(new XiaoFeiOrderConfirmObject(dealHistoryDetailPay.get(i).getId()));
					CommUtil.openSingleFragmentActivity(mBaseActivity, OrderDetailFrament.class);
				}
			});
		}
		dealHistoryDetailListAdapter.notifyDataSetChanged();
	}

	public static void setCloseInd(boolean closeInd) {
		WeDeOrdersFragment.closeInd = closeInd;
	}
	public static void setStatus(String status) {
		WeDeOrdersFragment.status = status;
	}

	/**
	 * @return the homeFragmentInstent
	 */
	public static WeDeOrdersFragment getHomeFragmentInstent() {
		return homeFragmentInstent;
	}

}
